package com.nanjia.boot.certificatetype.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nanjia.boot.certificatetype.converter.CertificateTypeConverter;
import com.nanjia.boot.certificatetype.mapper.CertificateTypeMapper;
import com.nanjia.boot.certificatetype.model.entity.CertificateType;
import com.nanjia.boot.certificatetype.model.form.CertificateTypeForm;
import com.nanjia.boot.certificatetype.model.query.CertificateTypeQuery;
import com.nanjia.boot.certificatetype.model.vo.CertificateTypeVO;
import com.nanjia.boot.certificatetype.service.CertificateTypeService;
import com.nanjia.boot.common.enums.StatusEnum;
import com.nanjia.boot.core.security.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;

/**
 * 证件类型服务实现类
 *
 * @author 周洋
 * @since 2025-06-13 10:14
 */
@Service
@RequiredArgsConstructor
public class CertificateTypeServiceImpl extends ServiceImpl<CertificateTypeMapper, CertificateType> implements CertificateTypeService {

    private final CertificateTypeConverter certificateTypeConverter;

    private final CertificateTypeMapper certificateTypeMapper;

    /**
     * 获取证件类型分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<CertificateTypeVO>} 证件类型分页列表
     */
    @Override
    public IPage<CertificateTypeVO> getCertificateTypePage(CertificateTypeQuery queryParams) {
        Page<CertificateTypeVO> pageVO = this.baseMapper.getCertificateTypePage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取证件类型表单数据
     *
     * @param id 证件类型ID
     * @return 证件类型表单数据
     */
    @Override
    public CertificateTypeForm getCertificateTypeFormData(Long id) {
        CertificateType entity = this.getById(id);
        return certificateTypeConverter.toForm(entity);
    }

    /**
     * 新增证件类型
     *
     * @param formData 证件类型表单对象
     * @param ipAddr
     * @return 是否新增成功
     */
    @Override
    public boolean saveCertificateType(CertificateTypeForm formData, String ipAddr) {
        LambdaQueryWrapper<CertificateType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(CertificateType::getId);
        List<CertificateType> list = this.list(queryWrapper);
        int x = 0;
        if (CollectionUtils.isNotEmpty(list)) {
            x = Integer.parseInt(list.get(0).getCertificateTypeCode());
        }
        CertificateType entity = certificateTypeConverter.toEntity(formData);
        entity.setCertificateTypeCode(String.format("%04d", x + 1));
        entity.setCreateBy(SecurityUtils.getUserStrId());
        entity.setCreateIp(ipAddr);
        entity.setCreateTime(LocalDateTime.now());
        return this.save(entity);
    }

    /**
     * 更新证件类型
     *
     * @param id       证件类型ID
     * @param formData 证件类型表单对象
     * @param ipAddr
     * @return 是否修改成功
     */
    @Override
    public boolean updateCertificateType(Long id, CertificateTypeForm formData, String ipAddr) {
        CertificateType entity = certificateTypeConverter.toEntity(formData);
        entity.setId(id);
        entity.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
        entity.setUpdateIp(ipAddr);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }

    /**
     * 删除证件类型
     *
     * @param ids 证件类型ID，多个以英文逗号(,)分割
     * @return 是否删除成功
     */
    @Override
    public boolean deleteCertificateTypes(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的证件类型数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        List<CertificateType> list = new ArrayList<>();
        for (Long l : idList) {
            CertificateType certificateType = new CertificateType();
            certificateType.setId(l);
            certificateType.setUpdateTime(LocalDateTime.now());
            certificateType.setUpdateBy(JSONUtil.toJsonStr(SecurityUtils.getUserId()));
            certificateType.setCertificateTypeStatus(StatusEnum.DELETE.getValue());
            list.add(certificateType);
        }
        return this.updateBatchById(list, idList.size());
    }

    @Override
    public List<CertificateType> getCertificateTypeList(CertificateTypeQuery queryParams) {
        Map<String, Object> map = new HashMap<>();
        map.put("certificate_type_status",queryParams.getCertificateTypeStatus());
        return certificateTypeMapper.selectByMap(map);
    }
}
